package sk.isomi.minihr.entities;

import java.util.Date;

import sk.isomi.enums.Gender;
import sk.isomi.helpers.DateHelper;
import sk.isomi.minihr.MiniHrConst;

public abstract class Person implements MiniHrConst {
	private String id;
	private String prefix;
	private String name;
	private String surname;
	private String birthNumber;
	private Date dateOfBirt; 
	private Gender gender;
	private Address address;
	
	
	public Person(String id, String prefix, String name, String surname, String birthNumber) { 
		this(id, surname, birthNumber);
		setPrefix(prefix);
		setName(name);
	}

	public Person(String id, String surname, String birthNumber) {
		setId(id);
		setSurname(surname);
		setBirthNumber(birthNumber);				
	}

	protected abstract boolean validateId(String id) throws RuntimeException;
	
	private void setId(String id) {
		if (validateId(id)) {
			this.id = id;
		}
				
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public int getAge() {
		return DateHelper.calcAge(getDateOfBirt());
	}
	
	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getBirtNumber() {
		return birthNumber;
	}

	public void setBirthNumber(String birtNumber) {
		this.birthNumber = birtNumber;
		this.dateOfBirt = DateHelper.parseBirtNumber(birthNumber);
		this.gender = DateHelper.parseGender(birthNumber);
	}

	public String getId() {
		return id;
	}

	public Date getDateOfBirt() {
		return dateOfBirt;
	}
	
	public Gender getGender() {
		return gender;
	}
	
	
	public void setAddress(Address address) {
		this.address = address;
	}

	public Address getAddress() {
		return address;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(String.format("Meno (ID): %s %s (%d)\n", getName(), getSurname(), getId()));
		builder.append(String.format("Datum narodenia: %s\n", DateHelper.formatDate(getDateOfBirt(), DATE_PATTERN)));
		builder.append(String.format("Pohlavie: %s\n", getGender()));
		return builder.toString();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null || ! (obj instanceof Person)) {
			return false;
		}
		return (getBirtNumber() != null) ? getBirtNumber().equals(((Person) obj).getBirtNumber()) : false; 
	}

	@Override
	protected void finalize() throws Throwable {
		System.out.println("finalizing person");
		super.finalize();
	}
	
	
}
